package site.longkui.consumer1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/consumer1")
public class userConsumerController {
    @Autowired
    private RestTemplate restTemplate; // 提供多种便捷访问远程http服务的方法，简单的restful服务模板
    /**
     * 消费者接口
     */
    @GetMapping("/getUser")
    public  String getUser(){
        //指出服务地址
        String url="http://service-provider1/user/getUser";
        //返回值类型和我们的业务返回值一致
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * 测试 带有 hystrix 断路器的接口
     *
     */
    @GetMapping("getNewUser1")
    public String getNewUser1(){
        //指出服务地址
        String url="http://service-provider-hystrix/user/getNewUser1";
        return restTemplate.getForObject(url, String.class);
    }
    @GetMapping("getNewUser2")
    public String getNewUser2(){
        //指出服务地址
        String url="http://service-provider-hystrix/user/getNewUser2";
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * 测试 hystrix开启熔断服务的接口
     */
    @GetMapping("getNewUser3/{id}")
    public String getNewUser3(@PathVariable("id") Integer id){
        String url="http://service-provider-hystrix/user/getNewUser3";
        url=url+"/"+id;
        return restTemplate.getForObject(url, String.class);
    }



}
